/*
 * @Author: zwf 240970521@qq.com
 * @Date: 2023-07-14 01:12:21
 * @LastEditors: zwf 240970521@qq.com
 * @LastEditTime: 2023-08-10 02:23:07
 * @FilePath: /esp32_cmake_test/main.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <stdio.h>
#include <string.h>
#include "adc.h"
#include "esp_log.h"
#include "esp_system.h"
#include "AS5600.h"
#include "esp_timer.h"
#include "motor.h"
#include "delay.h"
#include "modbusSlave.h"
#include "currentSensing.h"
#include "dataSensing.h"
#include "fluxObserver.h"
#include "smbObserver.h"
#include "IQmathLib.h"

/*核心板,i2c sda:23,scl:5,ua:26,ub:27,uc14,modbus:17,16*/
/*开发板,i2c sda:23,scl:18,ua:26,ub:27,uc:39,modbus:5,4*/

void app_main(void)
{
    AS5600Dev      *as5600   = NULL;
    esp_err_t       res      = ESP_OK;
    MotorDev       *motor    = NULL;
    MotorCfg        motorCfg = {0};
    CurrentSensing *sen      = NULL;

    I2C_init();
    res = AS5600_create(0, 23, 5, &as5600);
    if (res)
    {
        printf("as5600_create error\n");
    }

    res = CURRENT_create(&sen, 35, 34, 50, 1650, 0.001F);
    if (res)
    {
        printf("CURRENT_create error\n");
    }
    /*初始化ADC*/
    ADC_init();
    DATA_init();
    // SMO_observerInit();
    FLUX_observerInit();

    /*电机配置*/
    motorCfg.pin[0]          = 26;
    motorCfg.pin[1]          = 27;
    motorCfg.pin[2]          = 14;
    motorCfg.timer           = LEDC_TIMER_0;
    motorCfg.vMax            = 11.1F;
    motorCfg.modbusStartAddr = 1000;
    motorCfg.pp              = 7;
    /*创建电机*/
    MOTOR_create(motorCfg, &motor);
    motor->registerSensor(motor, as5600);
    /*校准0点电流*/
    sen->correctAmpere(sen);
    motor->registerAmpereSensing(motor, sen);
    motor->start(motor);

    /*modbus初始化*/
    MODBUS_slaveInit();

    while (1)
    {
        mdelay(1000);
    }
}
